/////////////////////////////////////////////////////////////////////////////
// Name:        updatepanel.cpp
// Purpose:     
// Author:      Brant Young
// Modified by: 
// Created:     Wed Jun 20 23:07:21 2007
// RCS-ID:      
// Copyright:   (C) Victsoft 2007
// Licence:     GPL 2.0
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"
#include "updatepanel.h"
#include "cfgmgr.h"



/*!
 * UpdatePanel constructors
 */

UpdatePanel::UpdatePanel(wxWindow* p_parent) : wxPanel(p_parent, ID_UPDATE_PANEL, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER|wxTAB_TRAVERSAL)
{
////@begin UpdatePanel content construction

    p_root_sizer = new wxBoxSizer(wxVERTICAL);
    this->SetSizer(p_root_sizer);

    p_auto_check_sizer_stc = new wxStaticBox(this, wxID_STATIC, _("Automatically check updates to:"));
    p_auto_check_sizer = new wxStaticBoxSizer(p_auto_check_sizer_stc, wxVERTICAL);
    p_root_sizer->Add(p_auto_check_sizer, 0, wxGROW|wxALL, 5);

    p_daodict_chkbox = new wxCheckBox( this, ID_DAODICT_CHKBOX, _("Daodict"), wxDefaultPosition, wxDefaultSize, 0 );
    p_daodict_chkbox->SetValue(false);
    p_auto_check_sizer->Add(p_daodict_chkbox, 0, wxGROW|wxLEFT|wxRIGHT|wxTOP, 5);

    p_addons_chkbox = new wxCheckBox( this, ID_ADDONS_CHKBOX, _("Installed Add-ons"), wxDefaultPosition, wxDefaultSize, 0 );
    p_addons_chkbox->SetValue(true);
    p_auto_check_sizer->Add(p_addons_chkbox, 0, wxGROW|wxALL, 5);

    p_updates_sizer_stc = new wxStaticBox(this, wxID_STATIC, _("When updates are found:"));
    p_updates_sizer = new wxStaticBoxSizer(p_updates_sizer_stc, wxVERTICAL);
    p_root_sizer->Add(p_updates_sizer, 0, wxGROW|wxALL, 5);

    p_ask_rbtn = new wxRadioButton( this, ID_ASK_RBTN, _("Ask me what I want to do"), wxDefaultPosition, wxDefaultSize, 0 );
    p_ask_rbtn->SetValue(false);
    p_updates_sizer->Add(p_ask_rbtn, 0, wxGROW|wxLEFT|wxRIGHT|wxTOP, 5);

    p_auto_rbtn = new wxRadioButton( this, ID_AUTO_RBTN, _("Automatically download and install update"), wxDefaultPosition, wxDefaultSize, 0 );
    p_auto_rbtn->SetValue(true);
    p_updates_sizer->Add(p_auto_rbtn, 0, wxGROW|wxLEFT|wxRIGHT|wxTOP, 5);

    p_warn_sizer = new wxBoxSizer(wxHORIZONTAL);
    p_updates_sizer->Add(p_warn_sizer, 0, wxGROW, 0);

    p_warn_sizer->Add(20, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_warn_chkbox = new wxCheckBox( this, ID_WARN_CHKBOX, _("Warn me if this will disable any of my add-ons"), wxDefaultPosition, wxDefaultSize, 0 );
    p_warn_chkbox->SetValue(false);
    p_warn_sizer->Add(p_warn_chkbox, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxBOTTOM, 5);

////@end UpdatePanel content construction
    InitEvents();
    
    init_cfg();
}



/*!
 * UpdatePanel destructor
 */

UpdatePanel::~UpdatePanel()
{
////@begin UpdatePanel destruction
////@end UpdatePanel destruction
}


void UpdatePanel::init_cfg() {
	bool flag;
	
	ConfigManager& cfg_mgr = ConfigManager::get();
	
	cfg_mgr.Read(_T("/Update/CheckDaodict"), &flag, true);
	p_daodict_chkbox->SetValue(flag);
	
	cfg_mgr.Read(_T("/Update/CheckAddons"), &flag, true);
	p_addons_chkbox->SetValue(flag);
	

    wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, 0);
    event.SetEventObject(this);
	
	long integer = cfg_mgr.Read(_T("/Update/FoundUpadtes"),long(1)); // 0 - ask, 1 - auto download and update
	if (0==integer) {
		p_ask_rbtn->SetValue(true);
		event.SetId(ID_ASK_RBTN);
	} else {
		p_auto_rbtn->SetValue(true);
		event.SetId(ID_AUTO_RBTN);
	}
	ProcessEvent(event);
	
	cfg_mgr.Read(_T("/Update/WarnIfUncompatible"),&flag, true);
	p_warn_chkbox->SetValue(flag);
}

bool UpdatePanel::set_cfg() {
	ConfigManager& cfg_mgr = ConfigManager::get();
	
	cfg_mgr.Write(_T("/Update/CheckDaodict"), p_daodict_chkbox->IsChecked());
	cfg_mgr.Write(_T("/Update/CheckAddons"), p_addons_chkbox->IsChecked());
	
	if (p_ask_rbtn->GetValue()) {
		cfg_mgr.Write(_T("/Update/FoundUpadtes"),0);
	} else {
		cfg_mgr.Write(_T("/Update/FoundUpadtes"),1);
	}
	
	cfg_mgr.Write(_T("/Update/WarnIfUncompatible"), p_warn_chkbox->IsChecked());
	
	return true;
}

void UpdatePanel::InitEvents() {
	this->Connect(ID_ASK_RBTN, wxEVT_COMMAND_RADIOBUTTON_SELECTED, 
			(wxObjectEventFunction)&UpdatePanel::OnUpdateType);
	this->Connect(ID_AUTO_RBTN, wxEVT_COMMAND_RADIOBUTTON_SELECTED, 
			(wxObjectEventFunction)&UpdatePanel::OnUpdateType);
}

void UpdatePanel::OnUpdateType(wxCommandEvent& event) {
	switch (event.GetId()) {
		case ID_ASK_RBTN:
			p_warn_chkbox->Disable();
			break;
		case ID_AUTO_RBTN:
			p_warn_chkbox->Enable();
			break;
		default :
			p_warn_chkbox->Disable();
	}
	
	event.Skip();
}

